<?php
namespace App\Admin\Metrics\Collecter;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;
use Illuminate\Support\Facades\DB;

class collectedStatus extends Donut
{
    protected $labels = ['待处理', '待发布', '已发布', '处理中', '处理失败'];
    protected $colors = ['#ea5455', '#21b978', '#d2d6de', '#ea5455', '#8b0000'];

    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('收集数据统计');
        $this->chartLabels($this->labels);
        // 设置图表颜色
        $this->chartColors($this->colors);
    }

    /**
     * 渲染模板
     *
     * @return string
     */
    public function render()
    {
        $this->fill();

        return parent::render();
    }

    /**
     * 写入数据.
     *
     * @return void
     */
    public function fill()
    {

        // ['待处理', '待发布', '已发布', '处理中', '处理失败'];
        $counts = DB::table('mini_collecter_collected')
            ->select(
                DB::raw('SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS todo_count'),
                DB::raw('SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS todo_publish_count'),
                DB::raw('SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS published_count'),
                DB::raw('SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS processing_count'),
                DB::raw('SUM(CASE WHEN status = 4 THEN 1 ELSE 0 END) AS process_fail_count'),
            )->first();

        $this->withContent($counts->todo_count, $counts->todo_publish_count, $counts->published_count, $counts->processing_count, $counts->process_fail_count);

        // 图表数据
        $this->withChart([$counts->todo_count, $counts->todo_publish_count]);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片头部内容.
     *
     * @return $this
     */
    protected function withContent($todo_count, $todo_publish_count, $published_count, $processing_count, $process_fail_count)
    {
        $style = 'margin-bottom: 8px';
        $labelWidth = 120;

        return $this->content(
            <<<HTML
<div style="display: flex;">
<div class="left">
    <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: {$this->colors[0]}"></i> {$this->labels[0]}
    </div>
    <div>{$todo_count}</div>
    </div>
    <div class="d-flex pl-1 pr-1" style="{$style}">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle" style="color: {$this->colors[1]}"></i> {$this->labels[1]}
        </div>
        <div>{$todo_publish_count}</div>
    </div>
    <div class="d-flex pl-1 pr-1" style="{$style}">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle" style="color: {$this->colors[2]}"></i> {$this->labels[2]}
        </div>
        <div>{$published_count}</div>
    </div>
</div>
<div class="right">
    <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle" style="color: {$this->colors[3]}"></i> {$this->labels[3]}
        </div>
        <div>{$processing_count}</div>
    </div>
    <div class="d-flex pl-1 pr-1" style="{$style}">
        <div style="width: {$labelWidth}px">
            <i class="fa fa-circle" style="color: {$this->colors[4]}"></i> {$this->labels[4]}
        </div>
        <div>{$process_fail_count}</div>
    </div>
</div>
</div>
HTML
        );
    }
}
